package com.engine.jucailinkq.attendance.component.persongroup.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.engine.jucailinkq.attendance.component.persongroup.cmd.*;
import com.engine.jucailinkq.attendance.component.persongroup.commonutil.PersongroupCommonUtil;
import com.engine.jucailinkq.attendance.component.persongroup.service.SchedulingResultsService;
import com.engine.jucailinkq.attendance.enums.CheckBoxEnum;
import com.engine.jucailinkq.attendance.enums.SchedulingApproachEnum;
import com.engine.jucailinkq.common.util.CommonUtil;
import com.engine.jucailinkq.common.util.DateUtil;
import com.engine.jucailinkq.common.util.DbTools;
import com.engine.jucailinkq.common.util.Utils;
import com.engine.core.impl.Service;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import weaver.general.BaseBean;
import weaver.general.Util;

import java.time.ZoneOffset;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
public class SchedulingResultsServiceImpl extends Service implements SchedulingResultsService {
    private  Map<String,Object> personGroupMap = PersongroupCommonUtil.getPersonGroup();
    private  Map<String,Object> departMentMap = PersongroupCommonUtil.getDepartMent();
    private  Map<String,Object> subCompanyMap = PersongroupCommonUtil.getSubCompany();
    private  Map<String,Object> classesInformationMap = PersongroupCommonUtil.getClassesInformation();
    private  Map<String,Object> candidateTeamMap = PersongroupCommonUtil.getCandidateTeam();

    @Override
    public Map<String, Object> queryDataTable(Map<String, Object> params) {
        String recurrence = Util.null2String(params.get("recurrence"));
        Map<String,Object> resultMap = Maps.newHashMap();
        int current = Integer.valueOf(Util.null2String(params.get("current")));
        int pageSize = Integer.valueOf(Util.null2String(params.get("pageSize")));
        String showAll = Util.null2String(params.get("showAll"));
        int startindex = (current-1)*pageSize;
        int endindex = current*pageSize;
//        Map<String,Object> personGroupMap = CommonUtil.getPersonGroup();
//        Map<String,Object> departMentMap = CommonUtil.getDepartMent();
//        Map<String,Object> subCompanyMap = CommonUtil.getSubCompany();
//        Map<String,Object> classesInformationMap = CommonUtil.getClassesInformation();
//        Map<String,Object> candidateTeamMap = CommonUtil.getCandidateTeam();

        BaseBean baseBean = new BaseBean();
//        baseBean.writeLog("queryDataTable params:" + JSONObject.toJSONString(params));
        if ("1".equals(recurrence)){
            //递归查询方式
            Map<String,Object> result = commandExecutor.execute(new GetDatatableRecurrenceCmd(params));
            baseBean.writeLog("queryDataTable resultA:" + JSONObject.toJSONString(result));
            List<Map<String, Object>> dataTableList = (List<Map<String, Object>>)result.get("dataTableList");
            if (!CheckBoxEnum.CHECKED.getKey().equals(showAll)){
                dataTableList = Utils.getSchedulingList(dataTableList);
            }
            dataTableList = dataTableList.stream().sorted(Comparator.comparing(e->DateUtil.getTime(e.get("bcrq").toString()).toInstant(ZoneOffset.of("+8")).toEpochMilli())).collect(Collectors.toList());
            int total = dataTableList.size();
            resultMap.put("total",total);
            if (pageSize != 9999){
                if (endindex > total){
                    endindex = total;
                }
                startindex = endindex-pageSize;
                if (startindex < 0){
                    startindex = 0;
                }
                dataTableList = dataTableList.subList(startindex,endindex);
            }
            for (Map<String, Object> map:dataTableList){
                SetBrowSerName(map);
            }
            resultMap.put("data",dataTableList);

        }else {
            //非递归查询方式
            Map<String,Object> result = commandExecutor.execute(new GetDatatableCountCmd(params));
            baseBean.writeLog("queryDataTable resultB:" + JSONObject.toJSONString(result));
            int total = Integer.valueOf(result.get("total").toString());
            params.put("total",total);
            resultMap = commandExecutor.execute(new GetDatatableCmd(params));
            List<Map<String,Object>> dataTable = (List<Map<String,Object>>)resultMap.get("data");
            for (Map<String,Object> map :dataTable){
                SetBrowSerName(map);

            }
            resultMap.put("total",total);
        }

        return resultMap;
    }

    /**
     * 塞入 人员、人员分组、部门、分部、班次信息、候选班组浏览按钮显示值
     * @param map
     */
    public void SetBrowSerName(Map<String,Object> map){
        log.debug("personGroupMap :{},departMentMap:{},subCompanyMap:{},classesInformationMap:{},candidateTeamMap:{}"
                ,personGroupMap.hashCode(),departMentMap.hashCode(),subCompanyMap.hashCode(),classesInformationMap.hashCode(),candidateTeamMap.hashCode());
        String dxlx = Util.null2String(map.get("dxlx"));
        if (dxlx.equals("0")){
            //人员
            String lastname = Util.null2String(map.get("lastname"));
            String pbdxry = Util.null2String(map.get("pbdxry"));
            pbdxry = pbdxry +"-"+lastname;
            map.put("pbdxry",pbdxry);

        }else if (dxlx.equals("1")){
            //人员分组
            String pbdxryfz = Util.null2String(map.get("pbdxryfz"));
            pbdxryfz= pbdxryfz+"-"+personGroupMap.get(pbdxryfz);
            map.put("pbdxryfz",pbdxryfz);

        }else if (dxlx.equals("2")){
            //部门
            String pbdxbm = Util.null2String(map.get("pbdxbm"));
            pbdxbm= pbdxbm+"-"+departMentMap.get(pbdxbm);
            map.put("pbdxbm",pbdxbm);

        }else if (dxlx.equals("3")){
            //分部
            String pbdxfb = Util.null2String(map.get("pbdxfb"));
            pbdxfb= pbdxfb+"-"+subCompanyMap.get(pbdxfb);
            map.put("pbdxfb",pbdxfb);
        }
        String bcxx = Util.null2String(map.get("bcxx"));
        String hxbz = Util.null2String(map.get("hxbz"));
        if (!"".equals(bcxx)){
            bcxx = bcxx +"-"+classesInformationMap.get(bcxx);
            map.put("bcxx",bcxx);
        }
        if (!"".equals(hxbz)){
            hxbz = hxbz +"-"+candidateTeamMap.get(hxbz);
            map.put("hxbz",hxbz);
        }
    }

    @Override
    public Map<String, Object> queryDataTableActualUse(Map<String, Object> params) {
        Map<String, Object> paramMap = queryDataTable(params);
        Map<String, Object> resultMap = Maps.newHashMap();
        List<Map<String, Object>> dataTableList = (List<Map<String, Object>>)paramMap.get("data");
        List<Map<String, Object>> resultList = Lists.newArrayList();
        resultMap.put("data",Utils.getSchedulingList(dataTableList));
        return resultMap;
    }

    @Override
    public Map<String, Object> saveScheduleResult(Map<String, Object> params) {
        String billid = Util.null2String(params.get("billid"));
        String sql = "select bc,rqlx,rq,ygid from uf_jcl_kq_cqjg where id=?";
        List<Map<String,Object>> dataList = DbTools.getSqlToList(sql,billid);
        Map<String,Object> dataMap = Maps.newHashMap();
        dataMap.put("pbtj",SchedulingApproachEnum.UPDATE.getKey());
        dataMap.put("dxlx","0");
        dataMap.put("pbdxry",dataList.get(0).get("ygid"));
        dataMap.put("bcrq",dataList.get(0).get("rq"));
        dataMap.put("rqlx",dataList.get(0).get("rqlx"));
        dataMap.put("sfxx", CommonUtil.ifrestByClass(dataList.get(0).get("bc").toString()));
        dataMap.put("sfdkpp", CheckBoxEnum.CHECKED.getKey());
        dataMap.put("bcxx",dataList.get(0).get("bc"));
        dataMap.put("pbrq",DateUtil.getCurrentDate());
        dataMap.put("pbsj",DateUtil.getCurrentTime("yyyy-MM-dd HH:mm").split(" ")[1]);

        Utils.InsertFormTable("uf_pbjg",dataMap,Utils.getFormmodeIdMap());

        return null;
    }

    @Override
    public Map<String, Object> generateSchedule(Map<String, Object> params) {
        return commandExecutor.execute(new GenerateScheduleCmd(params));
    }

    @Override
    public Map<String, Object> deleteBatchSchedule(Map<String, Object> params) {

        return commandExecutor.execute(new DeleteBatchScheduleCmd(params));
    }
}
